﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "入职申请添加";
    Layout = null;
}
@Html.AntiForgeryToken()

<link href="https://cdn.bootcdn.net/ajax/libs/layui/2.6.8/css/layui.min.css" rel="stylesheet">

<style>
    .custom-form {
        padding: 20px;
        background-color: #fff;
        box-shadow: 0 1px 2px 0 rgba(0,0,0,.05);
    }

    .page-header {
        margin-bottom: 20px;
        border-bottom: 1px solid #f6f6f6;
        padding-bottom: 10px;
    }

        .page-header h2 {
            font-size: 18px;
            color: #333;
            font-weight: 500;
        }

    .required:after {
        content: '*';
        color: red;
        margin-left: 4px;
    }

    .layui-input {
        min-height: 38px !important;
    }

    #interviewTime {
        height: 38px !important;
    }
</style>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="page-header">
                <h2><i class="layui-icon layui-icon-user"></i> 入职申请添加</h2>
            </div>

            <form class="layui-form custom-form" id="onboardingForm" lay-filter="onboardingForm">
                <div class="layui-form-item">
                    <label class="layui-form-label required">人员名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="personName" id="personName" required lay-verify="required" placeholder="请输入人员名称" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" id="phone" required lay-verify="required|phone" placeholder="请输入联系电话" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="sex" value="0" title="男" checked>
                        <input type="radio" name="sex" value="1" title="女">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">定级</label>
                    <div class="layui-input-block">
                        <select name="gradingMatch" lay-verify="required" lay-search>
                            <option value="">请选择定级</option>
                            <option value="1">1级</option>
                            <option value="2">2级</option>
                            <option value="3">3级</option>
                            <option value="4">4级</option>
                            <option value="5">5级</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label required">面试反馈</label>
                    <div class="layui-input-block">
                        <textarea name="interviewFeedback" id="interviewFeedback" placeholder="请输入面试反馈" class="layui-textarea" lay-verify="required"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">面试结果</label>
                    <div class="layui-input-block">
                        <select name="interviewResult" lay-verify="required">
                            <option value="">请选择面试结果</option>
                            <option value="通过">通过</option>
                            <option value="未通过">未通过</option>
                            <option value="待定">待定</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">面试次数</label>
                    <div class="layui-input-block">
                        <input type="number" name="nterviewNumber" id="nterviewNumber" min="1" lay-verify="required|number" placeholder="请输入面试次数" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">面试时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="interviewTime" id="interviewTime" lay-verify="required" placeholder="请选择面试时间" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">面试官</label>
                    <div class="layui-input-block">
                        <input type="text" name="interviewPeople" id="interviewPeople" lay-verify="required" placeholder="请输入面试官" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submitBtn">
                            <i class="layui-icon layui-icon-ok"></i> 提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-refresh"></i> 重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.6.8/layui.min.js"></script>

<script>
    // 获取URL参数
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }

    layui.use(['form', 'laydate', 'layer'], function(){
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;

        var recordId = getQueryVariable('id');

        // 初始化日期时间选择器
        laydate.render({
            elem: '#interviewTime',
            type: 'datetime'
        });

        // 如果是编辑模式，则获取数据并反填
        if (recordId) {
            $('.page-header h2').html('<i class="layui-icon layui-icon-edit"></i> 编辑入职申请');
            $.get('/OnboardingReview/GetOnboardingReviewById', { id: recordId }, function (data) {
                form.val('onboardingForm', data);
            });
        }

        // 表单验证
        form.verify({
            phone: [
                /^1[3-9]\d{9}$/,
                '请输入正确的手机号'
            ]
        });

        // 表单提交
        form.on('submit(submitBtn)', function(data){
            var loadIndex = layer.load(2); // 显示加载动画
            var postData = data.field;
            postData.__RequestVerificationToken = $('input[name="__RequestVerificationToken"]').val();

            $.ajax({
                url: '/OnboardingReview/AddOnboardingReview',
                type: 'POST',
                data: postData,
                success: function(res) {
                    layer.close(loadIndex);
                    console.log("服务器响应:", res);

                    if(res.code === 0 || res.success) {
                        // 兼容数字和字符串类型
                        if(res.code == 0 || res.success) {
                            layer.msg('添加成功');
                            window.location.href = '/OnboardingReview/OnboardingReviewListView';
                        } else {
                            layer.msg('添加失败：' + (res.msg || res.message || '未知错误'));
                        }
                    } else {
                        layer.msg('添加失败：' + (res.msg || res.message || '未知错误'));
                    }
                },
                error: function(xhr) {
                    layer.close(loadIndex);
                    layer.msg('请求失败: ' + xhr.responseText, { icon: 2 });
                }
            });
            return false;
        });
    });
</script>
